package com.kobeliu.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.kobeliu.base.BaseInfoProperties;
import com.kobeliu.exceptions.GraceException;
import com.kobeliu.mapper.ResumeWorkExpMapper;
import com.kobeliu.pojo.ResumeWorkExp;
import com.kobeliu.pojo.bo.EditWorkExpBO;
import com.kobeliu.result.ResponseStatusEnum;
import com.kobeliu.service.ResumeWorkExpService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class ResumeWorkExpServiceImpl extends BaseInfoProperties implements ResumeWorkExpService {

    @Autowired
    private ResumeWorkExpMapper workExpMapper;

    @Transactional
    @Override
    public void editWorkExp(EditWorkExpBO bo) {

        if(bo == null)
            GraceException.display(ResponseStatusEnum.RESUME_MAX_LIMIT_ERROR);

        ResumeWorkExp pending = new ResumeWorkExp();
        BeanUtils.copyProperties(bo,pending);
        pending.setUpdatedTime(LocalDateTime.now());

        if(StringUtils.isBlank(bo.getId())){
            //插入
            pending.setCreateTime(LocalDateTime.now());
            workExpMapper.insert(pending);
        }else{
            workExpMapper.updateById(pending);
        }

        //改动后删除resume的缓存
        redis.del(REDIS_RESUME_INFO+":"+pending.getUserId());
    }

    @Override
    public ResumeWorkExp getWorkExp(String workExpId, String userId) {
        ResumeWorkExp pending = workExpMapper.selectOne(new QueryWrapper<ResumeWorkExp>()
                .eq("user_id",userId)
                .eq("id",workExpId));

        return pending==null?new ResumeWorkExp():pending;
    }

    @Override
    public List<ResumeWorkExp> getWorkExps(String userId) {

        return workExpMapper.selectList(new QueryWrapper<ResumeWorkExp>()
                    .eq("user_id",userId));

    }

    @Transactional
    @Override
    public void deleteByWorkExpIdAndUserId(String workExpId, String userId) {

        workExpMapper.delete(new QueryWrapper<ResumeWorkExp>()
                .eq("id",workExpId)
                .eq("user_id",userId));

        redis.del(REDIS_RESUME_INFO+":"+userId);
    }
}
